package com.zuo.demo01.controller.admin;


import com.zuo.demo01.pojo.User;
import com.zuo.demo01.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
public class ChangeController {

    @Autowired
    private UserService userService;


    @RequestMapping("/update/user/password")
    @ResponseBody
    public String update_password(@RequestParam("id") Integer id, @RequestParam("password") String password, @RequestParam("npsd") String npsd, HttpSession session){

        //比较原密码
        User loginUser = (User) session.getAttribute("loginUser");
        if(!loginUser.getPassword().equals(password)){
            return "原密码错误";
        }

        Map<String,Object> map = new HashMap<>();
        map.put("password",npsd);
        map.put("id",id);
        int result = userService.update(map);

        if(result==1){
            //修改成功
            return "success";
        }else{
            return "error";
        }
    }

    @RequestMapping("/update/user/name")
    @ResponseBody
    public String update_name(@RequestParam("id") Integer id,@RequestParam("name") String name,@RequestParam("nickname") String nickname){
        Map<String,Object> map = new HashMap<>();
        map.put("id",id);
        map.put("username",name);
        map.put("nickname",nickname);
        int result = userService.update(map);
        if (result == 1) {
            return "success";
        }else{
            return "修改失败";
        }

    }



}
